<template>
  <el-dialog
    :title="!dataForm.id ? '新建' : isDetail ? '调期' : '编辑'"
    :close-on-click-modal="false"
    append-to-body
    :visible.sync="visible"
    class="JNPF-dialog JNPF-dialog_center"
    lock-scroll
    width="800px"
  >
    <el-row :gutter="15" class="">
      <el-form
        ref="elForm"
        :model="dataForm"
        :rules="rules"
        size="small"
        label-width="100px"
        label-position="right"
      >
        <template v-if="!loading">
          <el-col :span="12">
            <el-form-item label="任务类型" prop="tasktype">
              <el-select
                v-model="dataForm.tasktype"
                placeholder="请选择"
                disabled
                clearable
                :style="{ width: '100%' }"
              >
                <el-option
                  v-for="(item, index) in tasktypeOptions"
                  :key="index"
                  :label="item.fullName"
                  :value="item.enCode"
                  :disabled="item.disabled"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="任务内容" prop="taskcontent">
              <el-input
                v-model="dataForm.taskcontent"
                placeholder="请输入"
                disabled
                clearable
                :style="{ width: '100%' }"
              >
              </el-input>
            </el-form-item>
          </el-col>

          
          <el-col :span="12">
            <el-form-item label="责任领导" prop="dutyleader">
              <user-select
                v-model="dataForm.dutyleader"
                placeholder="请选择"
                disabled
                clearable
              >
              </user-select>
            </el-form-item>
          </el-col>
          
          <el-col :span="12">
            <el-form-item label="责任部门" prop="dutydepartment">
              <dep-select
                v-model="dataForm.dutydepartment"
                placeholder="请选择"
                disabled
                clearable
              >
              </dep-select>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="责任人" prop="dutyperson">
              <user-select
                v-model="dataForm.dutyperson"
                placeholder="请选择"
                disabled
                clearable
              >
              </user-select>
            </el-form-item>
          </el-col>


          <el-col :span="12">
            <el-form-item label="开始时间" prop="starttime">
              <el-date-picker
                v-model="dataForm.starttime"
                placeholder="请选择"
                @change="autoComputeTimeLong()"
                clearable
                :style="{ width: '100%' }"
                type="datetime"
                format="yyyy-MM-dd HH:mm:ss"
                value-format="timestamp"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="结束时间" prop="endtime">
              <el-date-picker
                v-model="dataForm.endtime"
                placeholder="请选择"
                @change="autoComputeTimeLong()"
                clearable
                :style="{ width: '100%' }"
                type="datetime"
                format="yyyy-MM-dd HH:mm:ss"
                value-format="timestamp"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="工期时间" prop="projecttime">
              <el-input
                v-model="dataForm.projecttime"
                placeholder="由开始时间、结束时间自动计算"
                readonly
                clearable
                :style="{ width: '100%' }"
              >
              </el-input>
            </el-form-item>
          </el-col>
        </template>
      </el-form>
    </el-row>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false"> 取 消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">
        提 交</el-button
      >
    </span>
  </el-dialog>
</template>
<script>
import { mapGetters } from "vuex"
import request from "@/utils/request";
import { getDataInterfaceRes } from "@/api/systemData/dataInterface";
import { getDictionaryDataSelector } from "@/api/systemData/dictionary";
export default {
  components: {},
  props: [],
  data() {
    return {
      visible: false,
      loading: false,
      isDetail: false,
      dataForm: {
        tasktype: "",
        taskcontent: "",
        starttime: "",
        endtime: "",
        projecttime: "",
        taskendtime: "",
        operator: "",
        taskstatus: "1",
        dutyperson: "",
        dutydepartment: "",
        dutyleader: "",
        creatoruserid: "",
        creatortime: "",
        lastmodifyuserid: "",
        lastmodifytime: "",
      },
      rules: {
        
        starttime: [
          {
            required: true,
            message: "请选择",
            trigger: "change",
          },
        ],
        endtime: [
          {
            required: true,
            message: "请选择",
            trigger: "change",
          },
        ],
      },
      tasktypeOptions: [],
      taskstatusOptions: [],
    };
  },
  computed: {
    ...mapGetters(['userInfo']),},
  watch: {},
  created() {
    this.gettasktypeOptions();
    // this.gettaskstatusOptions();
  },
  mounted() {},
  methods: {
    gettasktypeOptions() {
      getDictionaryDataSelector("316926148233659781").then((res) => {
        this.tasktypeOptions = res.data.list;
      });
    },
    // gettaskstatusOptions() {
    //   getDictionaryDataSelector("316926312189003141").then((res) => {
    //     this.taskstatusOptions = res.data.list;
    //   });
    // },
    clearData(data) {
      for (let key in data) {
        if (data[key] instanceof Array) {
          data[key] = [];
        } else if (data[key] instanceof Object) {
          this.clearData(data[key]);
        } else {
          data[key] = "";
        }
      }
    },
    init(id, isDetail) {
      this.dataForm.id = id || 0;
      this.visible = true;
      this.isDetail = isDetail || false;
      this.$nextTick(() => {
        this.$refs["elForm"].resetFields();
        if (this.dataForm.id) {
          this.loading = true;
          request({
            url: "/api/example/BusTask/" + this.dataForm.id,
            method: "get",
          }).then((res) => {
            this.dataInfo(res.data);
            this.loading = false;
          });
        } 
      });
      this.$store.commit("generator/UPDATE_RELATION_DATA", {});
    },

    // // 更改责任人,同时需要修改责任部门、责任领导的默认值
    // changeDutyPerson(userId){
    //   request({
    //     url: "/api/example/BusTask/getDutyPersonInfo/" + userId,
    //     method: "get",
    //   }).then((res) => {
    //     this.dataForm.dutyperson = userId;
    //     this.dataForm.dutydepartment=res.data.organizeId;
    //     this.dataForm.dutyleader=res.data.managerId;
    //   });

    // },
    
    // 自动计算工期时间
    matchDateTime(startTime,endTime){
        if(endTime<startTime && startTime>0 && endTime>0){
            return true;
        }else{
            return false;
        }
    },

    autoComputeTimeLong(){
        var starttime = this.dataForm.starttime;
        starttime = starttime?starttime:0;
        var endtime = this.dataForm.endtime;
        endtime = endtime?endtime:0;
        
        if(this.matchDateTime(starttime,endtime)){
            this.$message({
            showClose: true,
            message: "结束时间小于开始时间!",
            type: "error",
            duration: 1000,
            });
            this.dataForm.projecttime = 0;
        }else{
            if(starttime<=endtime && starttime>0 && endtime>0){
                // console.log(starttime);
                // console.log(endtime);
                // console.log(endtime - starttime);
                // 按天数计算
                this.dataForm.projecttime = Math.floor((endtime - starttime)/1000/3600/24)+1;
            }else{
                this.dataForm.projecttime = 0;
            }
        }
    },

    // 表单提交
    dataFormSubmit() {
        
        //判断时间范围的合理性
        var starttime = this.dataForm.starttime;
        starttime = starttime?starttime:0;
        var endtime = this.dataForm.endtime;
        endtime = endtime?endtime:0;
         if(this.matchDateTime(starttime,endtime)){
            this.$message({
            showClose: true,
            message: "结束时间小于开始时间!",
            type: "error",
            duration: 1000,
            });
            return;
        }

      this.$refs["elForm"].validate((valid) => {
        if (valid) {
          this.request();
        }
      });
    },
    request() {
      var _data = this.dataList();
      if (!this.dataForm.id) {
        // request({
        //   url: "/api/example/BusTask",
        //   method: "post",
        //   data: _data,
        // }).then((res) => {
        //   this.$message({
        //     message: res.msg,
        //     type: "success",
        //     duration: 1000,
        //     onClose: () => {
        //       this.visible = false;
        //       this.$emit("refresh", true);
        //     },
        //   });
        // });
      } else {
        request({
          url: "/api/example/BusTask/tqTask/" + this.dataForm.id,
          method: "PUT",
          data: _data,
        }).then((res) => {
          this.$message({
            message: res.msg,
            type: "success",
            duration: 1000,
            onClose: () => {
              this.visible = false;
              this.$emit("refresh", true);
            },
          });
        });
      }
    },
    dataList() {
      var _data = JSON.parse(JSON.stringify(this.dataForm));
      return _data;
    },
    dataInfo(dataAll) {
      let _dataAll = dataAll;
      this.dataForm = _dataAll;
    },
  },
};
</script>
